<template>
  <a-modal
    v-model="visible"
    title="要以什么信息打印胸卡？"
    :maskClosable="false"
    centered
    :afterClose="afterClose"
    width="600px"
  >
    <a-radio-group v-model="printType">
      <a-radio :value="1">按报名信息打印</a-radio>
      <a-radio :value="2" :disabled="disabledPrintType">按代签到人信息打印</a-radio>
    </a-radio-group>
    <PrintDeviceSelect :activityId="activityId" @change="printDeviceChange" />
    <template #footer>
      <Button @click="closeModel">取消</Button>
      <Button type="primary" @click="() => submit()">仅打印</Button>
      <Button type="primary" @click="() => submit(true)">打印并代签到</Button>
    </template>
  </a-modal>
</template>

<script>
import PrintDeviceSelect from './printDeviceSelect.vue'
import { printChestCard } from '@/api/activity/enroll'
import { Button } from 'ant-design-vue'
export default {
  props: {
    // 活动ID
    activityId: {
      type: Number | String
    },
    // 报名ID
    registerId: {
      type: Number
    },
    //报名人员数据
    record: {
      type: Object
    }
  },
  components: {
    PrintDeviceSelect,
    Button
  },
  computed: {
    disabledPrintType() {
      let { record } = this
      if (record.signType === 1 || record.signStatus === 0) {
        return true
      } else if (
        record.signType &&
        !record.replaceSignCompanyName &&
        !record.replaceSignCompanyPosition &&
        !record.replaceSignPersonName &&
        !record.replaceSignPersonPhone
      ) {
        return true
      }
      return false
    }
  },
  data() {
    return {
      visible: false,
      currentPrintDevice: undefined,
      options: [],
      printType: 1
    }
  },
  methods: {
    openModel() {
      this.visible = true
    },
    closeModel() {
      this.visible = false
    },
    afterClose() {
      this.printType = 1
      this.$emit('resetCheckedRows')
    },
    printDeviceChange(item){
      this.currentPrintDevice = item;
    },
    async submit(isSign = false) {
      if (!this.currentPrintDevice) return this.$message.warning('请选择打印设备')
      await printChestCard({
        equipmentNumber: this.currentPrintDevice.printerNo,
        deviceId: this.currentPrintDevice.id,
        activityId: this.activityId,
        registerId: this.registerId,
        signType: this.printType,
        isSign
      })
      this.$message.success('打印成功')
      this.closeModel()
      this.$emit('ok')
    }
  }
}
</script>

<style></style>
